package cn.edu.ujn.springboot.controller;

import cn.edu.ujn.dao.ParkingGuidanceCarParkInfo;
import cn.edu.ujn.dao.ParkingGuidanceInfo;
import cn.edu.ujn.service.IParkingGuidanceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/parkingGuidances")
public class ParkingGuidanceInfoController {

    @Autowired
    private IParkingGuidanceInfoService parkingGuidanceInfoService;

    @PostMapping
    public String addParkingGuidance(@RequestBody ParkingGuidanceInfo parkingGuidanceInfo) {
        System.out.println("00000");
        try {
            int result = parkingGuidanceInfoService.insertParkingGuidance(parkingGuidanceInfo);
            if (result > 0) {
                return "Parking Guidance added successfully!";
            } else {
                return "Failed to add Parking Guidance.";
            }
        } catch (Exception e) {
            return "Error occurred: " + e.getMessage();
        }
    }
    @PutMapping
    public String updateParkingGuidance(@RequestBody ParkingGuidanceInfo parkingGuidanceInfo) {
        try{
            int result = parkingGuidanceInfoService.updateParkingGuidance(parkingGuidanceInfo);
            if(result > 0){
                return "Parking Guidance updated successfully!";
            }else{
                return "Failed to update Parking Guidance.";
            }
        }catch (Exception e){
            return "Error occurred: " + e.getMessage();
        }
    }

    @DeleteMapping("/{id}")
    public String deleteParkingGuidance(@PathVariable int id) {
        try {
            int result = parkingGuidanceInfoService.deleteParkingGuidance(id);
            if (result > 0) {
                return "Parking Guidance deleted successfully!";
            } else {
                return "Failed to delete Parking Guidance.";
            }
        } catch (Exception e) {
            return "Error occurred: " + e.getMessage();
        }
    }

    @GetMapping
    public List<ParkingGuidanceInfo> getAllParkingGuidances() {
        return parkingGuidanceInfoService.getAllParkingGuidances();
    }

    // Get a Parking Guidance by ID
    @GetMapping("/{id}")
    public ParkingGuidanceInfo getParkingGuidanceById(@PathVariable int id) {
        return parkingGuidanceInfoService.getParkingGuidanceById(id);
    }

    @GetMapping("/list")
    public List<ParkingGuidanceCarParkInfo> getCarParks(@RequestParam String keyword) {
        return parkingGuidanceInfoService.getCarParksByKeyword(keyword);
    }

    // 获取停车场总数
    @GetMapping("/totalParkingLots")
    public int getTotalParkingLots() {
        return parkingGuidanceInfoService.getTotalParkingLots();
    }

    // 获取停车位利用率
    @GetMapping("/parkingSpaceUsageRate")
    public double getParkingSpaceUsageRate() {
        return parkingGuidanceInfoService.getParkingSpaceUsageRate();
    }

    // 获取实时空余车位
    @GetMapping("/timelyAvailableSpaces")
    public int getTimelyAvailableSpaces() {
        return parkingGuidanceInfoService.getTimelyAvailableSpaces();
    }



}
